unit LookupF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, FMTBcd, SqlExpr, Provider, DB, DBClient,
  DBGridA, SQLDataSetA;

type
  TLookupFrame = class(TFrame)
    Grid: TDBGrid;
    Source: TDataSource;
    Ds: TClientDataSet;
    procedure GridCellClick(Column: TColumn);
  private
    FNotifySelection: TNotifyEvent;
    Filter: String;
    procedure OpenDefaultList(Sender: TObject);
    procedure SetFilter(Filter: String);
    procedure SetSearchString(const Value: String);
  protected
    PrintableKeyPressed: Boolean;
    FSearchString: String;
    procedure ReFilter;
  public
    constructor Create(AOwner: TComponent; NotifySelection: TNotifyEvent); reintroduce; virtual;
    procedure TargetKeyPress(Key: Char; CurrentText: String);
    procedure TargetKeyUp(Key: Word; CurrentText: String);
    procedure TargetKeyDown(Key: Word; CurrentText: String);
    property SearchString: String read FSearchString write SetSearchString;
    procedure DropDown(Left, Top, Width: Integer);
    procedure CloseUp;
  end;

implementation
uses
  ProgramM, DebugFrm, Utils;

{$R *.dfm}

{ TLookupFrame }

procedure TLookupFrame.CloseUp;
begin
  Self.Visible := False;
end;

constructor TLookupFrame.Create(AOwner: TComponent; NotifySelection: TNotifyEvent);
begin
  inherited Create(AOwner);
  PrintableKeyPressed := False;
  FNotifySelection := NotifySelection;
  Grid.ScrollBars := ssNone;
end;



procedure TLookupFrame.TargetKeyDown(Key: Word; CurrentText: String);
begin
  Assert(Self.Visible);
  
  PrintableKeyPressed := False;
  if Key in [VK_RETURN, VK_TAB] then
  begin
    CloseUp;
    FNotifySelection(Self);
  end
  else if Key = VK_ESCAPE then
  begin
    CloseUp;
    Abort;
  end
  else if Key = VK_DOWN then
  begin
    Ds.FindNext;
    Abort;
  end
  else if Key = VK_UP then
  begin
    Ds.FindPrior;
    Abort;
  end;
end;

procedure TLookupFrame.TargetKeyPress(Key: Char;  CurrentText: String);
begin
  Assert(Self.Visible);

  if Key in Utils.NAME_CHARS then
  begin
    PrintableKeyPressed := True;
    SearchString := CurrentText + Key;
  end;
end;

procedure TLookupFrame.TargetKeyUp(Key: Word; CurrentText: String);
begin
  Assert(Self.Visible);
  
  if PrintableKeyPressed or (Key in [VK_DELETE, VK_BACK]) then
  begin
    Self.SearchString := CurrentText;
  end;
end;

procedure TLookupFrame.OpenDefaultList(Sender: TObject);
begin
  Self.SearchString := '';
end;




procedure TLookupFrame.ReFilter;
begin
  Ds.DisableControls;
  Ds.Filtered := False;
  Ds.Filtered := True;
  Ds.EnableControls;
end;

procedure TLookupFrame.SetFilter(Filter: String);
begin
  Self.Filter := Filter;
end;

procedure TLookupFrame.SetSearchString(const Value: String);
begin
  FSearchString := Value;
  ReFilter;
end;

procedure TLookupFrame.DropDown(Left, Top, Width: Integer);
begin
  Self.Left := Left;
  Self.Top := Top;
  Self.Width := Width;
  Self.Visible := True;
end;

procedure TLookupFrame.GridCellClick(Column: TColumn);
begin
  CloseUp;
  FNotifySelection(Self);
end;


end.
